﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Photoshop Fillmode
    /// </summary>
    public class HartesLicht : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// 
        /// </summary>
        public HartesLicht()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmapSource"></param>
        /// <param name="_bitmapDest"></param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmapSource, UnsafeBitmap _bitmapDest)
        {
            UnsafeBitmap bitmapSource = _bitmapSource;
            UnsafeBitmap bitmapDest = _bitmapDest;

            Color color = Color.Transparent;

            int y = 0;
            int x = 0;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = bitmapSource.GetPixel(x, y);

                    float nR = color.R;
                    float nG = color.G;
                    float nB = color.B;

                    float nRedDiv = (255.0f - nR); if (nRedDiv == 0) nRedDiv = 1;
                    float nGreenDiv = (255.0f - nG); if (nGreenDiv == 0) nGreenDiv = 1;
                    float nBlueDiv = (255.0f - nB); if (nBlueDiv == 0) nBlueDiv = 1;
                    nR = color.R / nRedDiv;
                    nG = color.G / nGreenDiv;
                    nB = color.B / nBlueDiv;
                    color = Color.FromArgb((int)nR, (int)nG, (int)nB);
                    bitmapDest.SetPixel(x, y, color);
                }
            }
            bitmapSource.Dispose();
            bitmapDest.Dispose();
            return _bitmapDest;
        }

        #endregion
        public override object Clone()
        {
            return new HartesLicht();
        }
    }

}
